package com.gjzhao.fmmall.validator;

import com.gjzhao.fmmall.annotation.FlagValidator;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Description: 状态标识 校验实现类
 * - 传入：注解名字/参数类型
 * @Author: gjzhao
 * @Date Created in 2022-01-24 11:22 上午
 */
public class FlagValidatorImpl implements ConstraintValidator<FlagValidator, Integer> {

    private String[] values;

    // 获取注解值[指定的可选值]
    @Override
    public void initialize(FlagValidator flagValidator) {
        this.values = flagValidator.value();
    }

    // 加入校验逻辑
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {

        boolean isValid = false;

        if (value == null) {
            //当状态为空时使用默认值：{}
            return true;
        }
        // 判断参数值是否在可选值中
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(String.valueOf(value))) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }
}
